/* 
 * File:   Rectangle.cpp
 * Author: mauro
 * 
 * Created on 13 de octubre de 2014, 18:27
 */

#include "Rectangle.h"
#include "Player.h"
//#include "SkinFactory.h"

Rectangle::Rectangle() {
}

Rectangle::Rectangle(ObjFactory *objFactory, bool flagStatic, float posX, float posY, float w, float h, float mass, float rotation, int idImage) {
    SBSLog* myLogFile;
    myLogFile->getInstance(MY_LOG_MODE);
    if (w < 0 || h < 0) {
        myLogFile->writeInLog(2, "[2] Tamaño del rectangulo invalido (Base o altura negativo)\n");
    }
    Dot *dot = new Dot(posX, posY, w, h);
    //    this->dot = dot;       
    Object *object = objFactory->getObject(flagStatic, dot->dotPixelsToWorld(), dot->sizeDotPixelsToWorld(), 0, mass, 4, rotation);
    this->object = object;
    myLogFile->writeInLog(0, "[0] Se ha creado un rectangulo exitosamente\n");

    if (idImage == 7) {     
        this->object->getBody()->GetFixtureList()->SetFriction(0); 
    }

    struct ObjectInit auxData;
    auxData.objectId = RECTANGLE;
    auxData.imageId = idImage;
    auxData.estatico = flagStatic;
    auxData.x = posX;
    auxData.y = posY;
    auxData.height = h;
    auxData.width = w;
    auxData.rot = rotation;

    auxData.idCharacter = VACIO;
    auxData.stateCharacter = VACIO;

    this->data = auxData;
    //nuevo flag
    this->equals = false;
    delete dot;
}

void Rectangle::restartPlayer(){}

void Rectangle::checkChanges() {
    b2Vec2 pos = this->object->getPosition();
    if (this->data.x == pos.x && this->data.y == pos.y && this->data.rot == this->object->getAngle()) {
        this->equals = true;
    } else {
        this->equals = false;
    }

}

void Rectangle::updatePosition() {
    b2Vec2 pos = this->object->getPosition();
    this->data.x = pos.x;
    this->data.y = pos.y;

}

void Rectangle::updateRotation() {
    this->data.rot = this->object->getAngle();
}

void Rectangle::updateData() {
    this->updatePosition();
    this->updateRotation();
}

struct ObjectInit Rectangle::getData() {
    this->updateData();
    return this->data;
}

b2Vec2 Rectangle::getPosition() {
    return this->object->getPosition();
}

float Rectangle::getAngle() {
    return this->object->getAngle();
}

float Rectangle::getAngularVelocity() {
    return this->object->getAngularVelocity();
}

void Rectangle::setAngularVelocity(float velocity) {
    this->object->setAngularVelocity(velocity);
}

void Rectangle::setUbicDyn(int id) {
    this->data.ubicDyn = id;
}

bool Rectangle::getAreEquals() {
    return this->equals;
}

void Rectangle::changeGroupIndex(int newGroup) {
    b2Body* body = this->object->getBody();
    b2Fixture* fixt = body->GetFixtureList();
    b2Filter filter = fixt->GetFilterData();
    filter.groupIndex = newGroup;
    fixt->SetFilterData(filter);
}

b2Body* Rectangle::getBody() {
    return this->object->getBody();
    
}

Rectangle::~Rectangle() {
    delete this->object;
    //    printf("Destructor de Rectangle\n");
}
